<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 7/18/2016
 * Des: 系统管理员
 */
defined('INHkMvc') or exit('Access Invalid!');

class App_member_vipModel extends AppModel
{   
    public function getMemberVipStatusByMemberId($member_id,$fields=''){
        return $this->field($fields)->find(['member_id'=>$member_id]);
    } 
    public function updateMemberVipStatus($member_vip, $uid)
    {
        $id = intval($member_vip['id']);
        unset($member_vip['id']);
        $member_id = $this->field('member_id')->find(['id' => $id]);
        $zone = M('app.ad_zone')->find(['member_id' => $member_id['member_id'], 'site_name' => 'dekahome']);
        $data = [];
        if ($member_vip['status'] == 3) {
            if ($zone !== false) {
                $ad = $zone['ad_zone_id'];
            } else {
                $ad = M('app.ad_zone')->distributionAdZoneId($member_id['member_id']);
                if ($ad === false) {
                    return false;
                }
                $res = M('app.member')->update(['ad_zone_id' => $ad], ['member_id' => $member_id['member_id']]);
                if ($res === false) {
                    return false;
                }
            }
            $data['ad_zone_id'] = $ad;
            $data['level'] = intval($member_vip['upgrade_level']);
        }
        $data['status'] = intval($member_vip['status']);
        $data['confirm_time'] = time();
        $data['mark'] = trim($member_vip['mark']);
        $data['admin_uid'] = $uid;
        $res = $this->update($data, ['id' => $id]);
        if ($res === false) {
            return false;
        }
        return true;
    }

    public function editMemberVipInfo($member_vip, $uid)
    {
        $id = intval($member_vip['id']);
        if ($member_vip['status'] == 3) {
            $data = [
                'status' => 1,
                'level' => intval($member_vip['upgrade_level']),
                'upgrade_level' => 0,
                'mark' => trim($member_vip['mark']),
                'admin_uid' => $uid,
                'confirm_time' => time()
            ];
        } else {
            $data = [
                'status' => 1,
                'upgrade_level' => 0,
                'admin_uid' => $uid,
                'mark' => trim($member_vip['mark']),
                'confirm_time' => time()
            ];
        }
        $res = $this->update($data, ['id' => $id]);
        if ($res > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function getMemberVipLevelByAdZoneId($ad_zone_id)
    {
        $member_id = M('app.ad_zone')->field('member_id')->find(['ad_zone_id' => $ad_zone_id])['member_id'];
        $tables = ['app.member_vip_level'];
        $where = ['vr.member_vip.member_id' => $member_id,
            'vr.member_vip.level' => ['vr.member_vip_level.level']];
        return $this->table($tables)->field('level')->find($where);
    }

    public function editOrderTime($order_time, $member_id)
    {
        if (intval($member_id) == 0) {
            return false;
        }
        $time = $this->field('order_time')->find(['member_id' => $member_id])['order_time'];
        if (!empty($time)) {
            return true;
        } else {
            return $this->update(['order_time' => $order_time, 'is_order' => 1], ['member_id' => $member_id]);
        }
    }
    /**
     * 申请提现
     * @param $money
     * @param $member_id
     * @param $account_num
     * @param $mobile
     * @return bool|mixed
     */
    public function applyMemberAccount($money, $member_id, $account_num, $mobile,$member_integration_id=0)
    {
        $apply = $this->getMemberVipStatusByMemberId($member_id,'total,balance,freeze,used');
        if ($apply['total'] > 0 && $apply['balance'] >= $money) {
            $this->begin();
            $apply_money['freeze'] = $apply['freeze'] + $money;
            $apply_money['balance'] = $apply['balance'] - $money;
            $apply_money['mobile'] = $mobile;
            $apply_money['ali_account'] = $account_num;
            $res = $this->update($apply_money, ['member_id' => $member_id]);
            if ($res === false) {
                $this->rollback();
                return false;
            }
            if($member_integration_id!=0){
                $total_member_integration=0;
                $total_integral_constraint=0;
                $member_integration=M('app.member_integration')->field('integral_voucher,integral_constraint')->select(['id'=>['in',$member_integration_id]]);
                foreach ($member_integration as $k => $v) {
                    $total_member_integration+=$v['integral_voucher'];
                    $total_integral_constraint+=$v['integral_constraint'];
                }
                if($money<$total_integral_constraint){
                    $this->rollback();
                    return false;
                }
                $res=M('app.member_integration')->update(['is_used'=>1],['id'=>['in',$member_integration_id]]);
                if ($res === false) {
                    $this->rollback();
                    return false;
                }
            }
            $update_log = M('app.member_balance_apply')->add($member_id, $money, $account_num,$member_integration_id);
            if ($update_log === false) {
                $this->rollback();
                return false;
            }
            $running_no = M('app.member_balance_apply')->field('running_no')->find(['id' => $update_log])['running_no'];
            if ($running_no === false) {
                $this->rollback();
                return false;
            }
            $this->commit();
            $data = $this->getMemberVipAccount($member_id);
            $data['running_no'] = $running_no;
            return $data;
        } else {
            return false;
        }
    }
    /**
     * 获取VIP账户
     * @param $member_id
     * @return mixed
     */
    public function getMemberVipAccount($member_id)
    {
        return $this->field('total,balance,unbalance,freeze,used,ali_account,est_income,rights_tb_est_income,rights_total_income')->find(['member_id' => $member_id]);
    }
    public function getMemberVip($member_id)
    {
        $tables = ['member_vip_level'];
        $where = ['level' => ['member_vip_level.level'],
            'member_id' => $member_id];
        return $this->table($tables)->field('status,level,confirm_time,month,member_vip_level.month time')->find($where);
    }
    public function applyExperienceVip($member_id){
        $member_vip=[];
        $member_vip['member_id'] = $member_id;
        $member_vip['month'] = 12;
        $member_vip['status'] =1;
        $member_vip['upgrade_level'] =0;
        $member_vip['level'] =1;
        $member_vip['apply_type'] = 0;
        $member_vip['apply_time'] = time();
        $member_vip['confirm_time'] = time();
        $member_vip['admin_uid'] = 1;
        $member_vip['invite_admin_uid'] = 1;
        $ad_zone_id= M('app.ad_zone')->distributionAdZoneId($member_id);
        if($ad_zone_id==false || $ad_zone_id===false){
            return $ad_zone_id;
        }
        $member_vip['ad_zone_id'] = $ad_zone_id;
        $res=$this->insert($member_vip);
        if($res===false){
           return $false; 
        }else{
            return M('app.member')->update(['ad_zone_id'=>$ad_zone_id],['member_id'=>$member_id]);
        }
    }
}
